#include "mainwindow.h"

#include <QApplication>

#include <QAction>
#include <QToolBar>

#include "urltasks.h"
#include "urltasksview.h"
#include "newtaskdialog.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_tasks(new UrlTasks(this))
    , m_view(new UrlTasksView(m_tasks, this))
{
    create_actions();
    create_toolbar();

    setCentralWidget(m_view);
}

void MainWindow::create_actions()
{
    m_quit = new QAction(QIcon(":/icons/quit"), tr("quit", "application control"), this);
    m_quit->setMenuRole(QAction::TextHeuristicRole);
    m_quit->setToolTip(tr("Push to exit application", "application control"));
    m_quit->setShortcut(QKeySequence::Quit);
    connect(m_quit, SIGNAL(triggered()), this, SLOT(quit()));

    m_new_task = new QAction(QIcon(":/icons/new"), tr("new", "download control"), this);
    m_new_task->setMenuRole(QAction::TextHeuristicRole);
    m_new_task->setToolTip(tr("Push this to start new download", "download control"));
    m_new_task->setShortcut(QKeySequence::New);
    connect(m_new_task, SIGNAL(triggered()), this, SLOT(new_task()));

    m_delete_task = new QAction(QIcon(":/icons/delete"), tr("delete", "download control"), this);
    m_delete_task->setMenuRole(QAction::TextHeuristicRole);
    m_delete_task->setToolTip(tr("Push this to delete download", "deonload control"));
    m_delete_task->setShortcut(QKeySequence::Delete);
    connect(m_delete_task, SIGNAL(triggered()), this, SLOT(delete_task()));

    m_start_task = new QAction(QIcon(":/icons/start"), tr("start", "download control"), this);
    m_start_task->setMenuRole(QAction::TextHeuristicRole);
    m_start_task->setToolTip(tr("Push this to start downloading", "download control"));
    m_start_task->setShortcut(QKeySequence::Redo);
    connect(m_start_task, SIGNAL(triggered()), this, SLOT(start_task()));

    m_stop_task = new QAction(QIcon(":/icons/stop"), tr("stop", "download control"), this);
    m_stop_task->setMenuRole(QAction::TextHeuristicRole);
    m_stop_task->setToolTip(tr("Push this to stop downloading", "download control"));
    m_stop_task->setShortcut(QKeySequence::Undo);
    connect(m_stop_task, SIGNAL(triggered()), this, SLOT(stop_task()));
}

void MainWindow::create_toolbar()
{
    QToolBar *task_control = addToolBar(tr("download control panel", "download control"));
    task_control->addAction(m_new_task);
    task_control->addAction(m_delete_task);
    task_control->addAction(m_start_task);
    task_control->addAction(m_stop_task);

    QToolBar *app_control = addToolBar(tr("application control panel", "application control"));
    app_control->addAction(m_quit);
}

void MainWindow::new_task()
{
    NewTaskDialog new_dialog(this);
    if (new_dialog.exec())
        m_tasks->create_new_task(new_dialog.get_url(), new_dialog.get_file());
}

void MainWindow::delete_task()
{
    QModelIndexList selection = m_view->selectionModel()->selectedRows();
    if (!selection.isEmpty()) m_tasks->delete_task_at(selection.first());
}

void MainWindow::start_task()
{
    QModelIndexList selection = m_view->selectionModel()->selectedRows();
    if (!selection.isEmpty())
    {
        UrlTask *task = static_cast<UrlTask *>(selection.first().internalPointer());
        task->start_task();
    }
}

void MainWindow::stop_task()
{
    QModelIndexList selection = m_view->selectionModel()->selectedRows();
    if (!selection.isEmpty())
    {
        UrlTask *task = static_cast<UrlTask *>(selection.first().internalPointer());
        task->stop_task();
    }
}

void MainWindow::quit()
{
    qApp->quit();
}
